package com.wanwen.controller;

import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanwen.dto.SigninDto;
import com.wanwen.entity.Asker;
import com.wanwen.entity.Signin;
import com.wanwen.mapper.AskerMapper;
import com.wanwen.mapper.SigninMapper;
import com.wanwen.utils.DateUtils;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;
import com.wanwen.utils.StringUtils;

@RestController
@RequestMapping("/signin")
public class SigninController {
	 @Autowired
		private SigninMapper signinMapper;
	 @Autowired
		private AskerMapper askerMapper;
	 /**
	  * /signin/getAll
	  * 查询
	  * @param signin
	  * @return rs
	 * @throws ParseException 
	  * */
	 @RequestMapping("/getAll")
	 public Object dynamicQuery(Integer page,Integer limit,String uid,String staTime,String endTime) {
	        QueryWrapper<SigninDto> queryWrapper = new QueryWrapper<>();
	        queryWrapper.eq(StringUtils.isnotNull(uid),"users.id", uid);
	        queryWrapper.ge(StringUtils.isnotNull(staTime),"signin.login_time",staTime);
	        queryWrapper.le(StringUtils.isnotNull(endTime),"signin.login_time",endTime);
	        queryWrapper.eq("signin.deleted", 0);
	        queryWrapper.eq("users.deleted", 0);
	        queryWrapper.apply("users.id = signin.uid");
	        Page<SigninDto> Page = new Page<SigninDto>(page,limit);
			IPage<SigninDto> signinPage = signinMapper.findSignin(Page, queryWrapper);
			return new ResultData(0, "", signinPage.getRecords(), signinPage.getTotal());
	    }
	 
	 /**
	  * /signin/add
	  * 签到
	  * @param signin
	  * @return rs
	 * @throws ParseException 
	  * */
	 @RequestMapping("/add")
	 public Object add(String id) throws ParseException {
		 QueryWrapper<Signin> SigninWrapper = new QueryWrapper<Signin>()
				.eq("uid", id)
				.inSql("DATE_FORMAT( login_time, \"%Y-%m-%d\" )", "CURDATE()");
		 Signin SigninOne = signinMapper.selectOne(SigninWrapper);
		 //判断是否签到
		 if(SigninOne==null) {
			 Signin entity = new Signin(null,Integer.parseInt(id),new Date(),null,DateUtils.isstate(),0);
			 int insert = signinMapper.insert(entity);
			 Asker asker = new Asker();
			 UpdateWrapper<Asker> updateWrapper = new UpdateWrapper<Asker>()
					 .set("remark", "已签到")
					 .eq("uid", id);
			askerMapper.update(asker, updateWrapper);
			 return new Result("签到成功", DateUtils.isstate(), insert==1?true:false);
		 }else {
			 return new Result("您已签到不能重复签到", "", false);
		 }
	 }  
	 /**
	  * /signin/update
	  * 签退
	  * @param signin
	  * @return rs
	  * */
	 @RequestMapping("/update")
	 public Object update(String id)throws ParseException {
		 QueryWrapper<Signin> SigninWrapper = new QueryWrapper<Signin>()
					.eq("uid", id)
					.inSql("DATE_FORMAT( login_time, \"%Y-%m-%d\" )", "CURDATE()");
			 Signin SigninOne = signinMapper.selectOne(SigninWrapper);
		if(SigninOne!=null) {
			
			if(SigninOne.getLoginOutTime()!=null){
				return new Result("您已签退，不能重复签退",null,false);
				}
			UpdateWrapper<Signin> SigninUpdateWrapper = new UpdateWrapper<Signin>()
					.set("login_out_time",new Date())
					.eq( "uid", id)
					.inSql("DATE_FORMAT( login_time, \"%Y-%m-%d\" )", "CURDATE()");
			int update = signinMapper.update(SigninOne, SigninUpdateWrapper);
			 Asker asker = new Asker();
			 UpdateWrapper<Asker> updateAskerWrapper = new UpdateWrapper<Asker>()
					 .set("remark", "已签退")
					 .eq("uid", id);
			askerMapper.update(asker, updateAskerWrapper);
			return new Result("签退成功",null,update==1?true:false);
		}else {
			return new Result("您还未签到，不能签退",null,false);
		}
	 }
	 
	 /**
	  * /signin/update
	  * 签退
	  * @param signin
	  * @return rs
	  * */
	 @RequestMapping("/updateSignin")
	 public Object updateSignin(Signin signin,String uname) {
		 QueryWrapper<Signin> SigninWrapper = new QueryWrapper<Signin>()
					.eq("uid", signin.getId())
					.isNull("login_out_time");
			 Signin SigninOne = signinMapper.selectOne(SigninWrapper);
		if(SigninOne!=null) {
			UpdateWrapper<Signin> SigninUpdateWrapper = new UpdateWrapper<Signin>()
					.set("login_out_time",new Date())
					.set("state","代签退")
					.eq( "uid", signin.getId())
					.isNull("login_out_time");
			int update = signinMapper.update(signin, SigninUpdateWrapper);
			return new  Result("签退成功",null,update==1?true:false);
		}else {
			return new Result(uname+"已签退",null,false);
		}
	 }
	 
}
